<template>
  <div class="page-container relative">
    <BaseCollapse title="基础信息">
      <DynamicForm :config="tsConfig" :questions="tsQuestions" ref="tsRef" />
    </BaseCollapse>

    <BaseCollapse title="文件信息">
      <DynamicForm :config="tpConfig" :questions="tpQuestions" ref="tpRef" />
      <AddIndex v-if="state.showType == 'ADD'"></AddIndex>
      <ReviseIndex v-else-if="state.showType == 'REVISE'"></ReviseIndex>
      <VoidIndex :creatorData="creatorRef" v-else-if="state.showType == 'VOID'"></VoidIndex>
    </BaseCollapse>
  </div>
</template>
<script setup lang="ts">
import HomeService from './index.service';
import AddIndex from '../component/addIndex.vue';
import ReviseIndex from '../component/reviseIndex.vue';
import VoidIndex from '../component/voidIndex.vue';
import BaseCollapse from '@/components/common/BaseCollapse/index.vue';
import { DynamicForm, DynamicTable } from '@/components/public-template';
import { reactive, onMounted, onBeforeUnmount, getCurrentInstance, provide, ref } from 'vue';
import { fileAuto } from '@/config/api/documentApplication';
import { filter } from 'rxjs/operators';
import { SELECTSERVE } from '@/mixins/subject/subject-types';

const { proxy }: any = getCurrentInstance();
const state = reactive({ showType: 'ADD' });
provide('showType', state);
const tsQuestions: any = reactive(HomeService.getQuestions());
const tpQuestions: any = reactive(HomeService.getQuestionsType());
const tsConfig: any = {
  labelCol: 8,
  wrapperCol: 16,
  grid: 6,
  layout: 'vertical',
  cancelSave: true
};
const tpConfig: any = {
  labelCol: 8,
  wrapperCol: 16,
  grid: 6,
  layout: 'vertical',
  cancelSave: true
};
const tsRef = ref(null);
const creatorRef = ref({});

onMounted(() => {
  if (state.showType == 'ADD') {
    fileAuto().then((result) => {
      if (result.success) {
        // 基本信息
        const data = result.data;
        tsQuestions.forEach((item) => (item.value = data[item.key]));
        creatorRef.value = tsRef.value.getDataFrom();
      }
    });
  }
});

const subWatch = proxy.$NotificationFormSub
  .getParams()
  .pipe(filter((res: any) => res !== 0))
  .subscribe((item: any) => {
    if (item.key === SELECTSERVE) {
      if (item.value.value == 1 && item.value.key == 'status') {
        state.showType = 'ADD';
      } else if (item.value.value == 2 && item.value.key == 'status') {
        state.showType = 'REVISE';
      } else if (item.value.value == 3 && item.value.key == 'status') {
        state.showType = 'VOID';
      }
    }
  });

onBeforeUnmount(() => {
  subWatch && subWatch.unsubscribe();
  proxy.$NotificationFormSub.clearParams();
});
</script>
<style lang="less" scoped>
.btn {
  margin-top: 100px;
}
</style>
